package com.haltakov.letterjumble.ui.components;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

import com.haltakov.letterjumble.Messages;
import com.haltakov.letterjumble.game.api.GameController;

public class GameButtons extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private JButton passButton;
	private JButton playButton;
	private JButton dictionaryButton;
	private JButton evaluateButton;

	public GameButtons(final GameController game) {
		super(new GridLayout(2, 2, 10, 15));
		
		setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		playButton = new JButton(Messages.getString("GameButtons.play")); //$NON-NLS-1$
		passButton = new JButton(Messages.getString("GameButtons.pass")); //$NON-NLS-1$
		dictionaryButton = new JButton(Messages.getString("GameButtons.check")); //$NON-NLS-1$
		evaluateButton = new JButton(Messages.getString("GameButtons.evaluate")); //$NON-NLS-1$
		
		playButton.setEnabled(false);
		passButton.setEnabled(false);
		dictionaryButton.setEnabled(false);
		evaluateButton.setEnabled(false);
		
		add(playButton);
		add(passButton);
		add(dictionaryButton);
		add(evaluateButton);
		
		
		playButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				game.played(game.getTempWord());
			}
			
		});
		
		passButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				game.played(null);
			}
			
		});
		
		dictionaryButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (game.getTempWord() == null)
					return;
				
				if (game.getDictionary().checkWord(game.getTempWord().getText()))
					game.showMessage(Messages.getString("GameButtons.word") + game.getTempWord().getText() + Messages.getString("GameButtons.figure")); //$NON-NLS-1$ //$NON-NLS-2$
				else
					game.showMessage(Messages.getString("GameButtons.doesnotexist") + game.getTempWord().getText() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
			}
			
		});
		
		evaluateButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (game.getTempWord() != null)
					game.showMessage(Messages.getString("GameButtons.word2") + game.getTempWord().getText() + Messages.getString("GameButtons.onposition") + game.getTempWord().getX() + "," + game.getTempWord().getY() + Messages.getString("GameButtons.give") + game.getTempWord().getScore() + Messages.getString("GameButtons.scores")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
			}
			
		});
	}

	public void enableAll() {
		playButton.setEnabled(true);
		passButton.setEnabled(true);
		dictionaryButton.setEnabled(true);
		evaluateButton.setEnabled(true);		
	}
}